package zhcms.admin;


import java.io.UnsupportedEncodingException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import zhcms.db.DBManager;
import zhcms.db.DbConn;
import zhcms.db.ConnectionPool.PooledConnection;
import zhcms.framework.Action;
import zhcms.framework.ActionForward;
import zhcms.tool.GyPage;

public class GyColumn implements Action  {

String pageStr="";
	
	public GyColumn()
	{
		
	}
	
	@Override
	public ActionForward excute(HttpServletRequest request,
			HttpServletResponse response) {
		return null;
	}
	
	public int listcount(){
		ResultSet rs = null;
		PooledConnection conn = null;
		String sql="select count(*) as id from `gyhy_column`";
		int countlyb=0;
		try {
			conn = DBManager.getConnection();
			rs = conn.executeQuery(sql);
			if (rs.next())
			{
				countlyb=rs.getInt("id");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally {
            conn.close();
		}
		return countlyb;
	}
	
	public ArrayList<HashMap<String, String>> getAllList(int currentpage)
	{
		int listnumcount=this.listcount();
		GyPage gyp=new GyPage();
		gyp.setP(20, listnumcount, "", "", currentpage);
		String limitStr=gyp.limit();
		pageStr=gyp.outPut();
		String sql="select * from `gyhy_column` order by ColumnCode asc limit "+limitStr;
		return DbConn.executeQuery(sql);
	}
	
	public ArrayList<HashMap<String, String>> getAllList()
	{
		String sql="select * from `gyhy_column` order by ColumnCode asc";
		return DbConn.executeQuery(sql);
	}
	
	public ArrayList<HashMap<String, String>> getOneList(int postid)
	{
		String sql="select * from `gyhy_column` where Id='"+postid+"'";
		return DbConn.executeQuery(sql);
	}
	
	public ArrayList<HashMap<String, String>> getOneList(String postid)
	{
		String sql="select * from `gyhy_column` where Id='"+postid+"'";
		return DbConn.executeQuery(sql);
	}
	
	public String getPageStr() {
		return pageStr;
	}

	public void setPageStr(String pageStr) {
		this.pageStr = pageStr;
	}
	
	public ActionForward save(HttpServletRequest request, HttpServletResponse response)
	{
		try {
			request.setCharacterEncoding("UTF-8");
			response.setCharacterEncoding("utf-8");
		} 
		catch (UnsupportedEncodingException e1) 
		{
			
		}
		String postid=getV(request.getParameter("postid"));
		//System.out.println("postid+++++++++++++"+postid);
		String Name=getV(request.getParameter("Name"));
		if(Name==null || Name.length()<=0 || Name.equals("null"))
		{
			return new ActionForward("/admin/admin_column.jsp",4,"栏目名称没有填写!",-1);
		}
		String ColumnCode=getV(request.getParameter("ColumnCode"));
		//System.out.println("ColumnCode+++++++++++++"+ColumnCode);
		String ColumnType=getV(request.getParameter("ColumnType"));
		if(ColumnType==null || ColumnType.length()<=0 || ColumnType.equals("null"))
		{
			return new ActionForward("/admin/admin_column.jsp",4,"栏目类型没有选择!",-1);
		}
		String IsColumn=getV(request.getParameter("IsColumn"));
		if(IsColumn==null || IsColumn.length()<=0 || IsColumn.equals("null"))
		{
			return new ActionForward("/admin/admin_column.jsp",4,"栏目所属没有选择!",-1);
		}
		String ColumnInfo=getV(request.getParameter("ColumnInfo"));
		String ParentCode=ColumnCode;
		String newColumnCode="";

		boolean EditCode=false;
		
		int StepLeng=4;
		
		String MatchStr="";
		
		for(int m=0;m<StepLeng;m++)
	    {
	        MatchStr=MatchStr+"_";
	    }
		
		PooledConnection conn = null;
		String sqlStr1="";
		String sqlStr="";
		
		if(postid==null || postid.length()<=0 || postid.equals("null"))
		{
			EditCode=true;
		}
		else
		{
			sqlStr="select ColumnCode from `gyhy_column` where Id="+postid;
		//	System.out.println(sqlStr);
			ArrayList<HashMap<String, String>> tempCode=new ArrayList<HashMap<String, String>>();
			tempCode=DbConn.executeQuery(sqlStr);
			Map<String,String> itempCode=new HashMap<String,String>();
			itempCode=tempCode.get(0);
			if(ParentCode.equals(itempCode.get("ColumnCode")))
			{
				EditCode=true;
			}
		}
		//System.out.println("EditCode+++++++++++++"+EditCode);
		if(EditCode==true)
		{
			sqlStr="Select ColumnCode from `gyhy_column` where ColumnCode like '"+ParentCode+MatchStr+"' order by ColumnCode desc";
		//	System.out.println("sqlStr+++++++++++++"+sqlStr);
			ArrayList<HashMap<String, String>> tempCode=new ArrayList<HashMap<String, String>>();
			tempCode=DbConn.executeQuery(sqlStr);
			//System.out.println("tempCode.size+++++++++++++"+tempCode.size());
			if(tempCode.size()>0)
			{
				Map<String,String> itempCode=new HashMap<String,String>();
				itempCode=tempCode.get(0);
				String SelfCode="";
				StringBuilder sb=new StringBuilder(itempCode.get("ColumnCode"));
				SelfCode=sb.reverse().toString();
			//	System.out.println("SelfCode+++++++++++++"+SelfCode);
			//	System.out.println("StepLeng+++++++++++++"+StepLeng);
				SelfCode=SelfCode.substring(0,StepLeng);
			//	System.out.println("SelfCode+++++++++++++"+SelfCode);
				StringBuilder sb1=new StringBuilder(SelfCode);
				SelfCode=sb1.reverse().toString();
				//SelfCode=itempCode.get("ColumnCode").substring((itempCode.get("ColumnCode").length()-StepLeng), StepLeng);
			//	System.out.println("ColumnCode+++++++++++++"+itempCode.get("ColumnCode"));
			//	System.out.println("SelfCode+++++++++++++"+SelfCode);
				String TeCode="";
				
				int iSelfCode=0;
				iSelfCode=Integer.parseInt(SelfCode)+2;
				//System.out.println("iSelfCode+++++++++++++"+iSelfCode);
				String SCode=Integer.toString(iSelfCode);
				for(int n=0;n<(StepLeng-SCode.length());n++)
				{
					TeCode=TeCode+"0";
				}
				TeCode=TeCode+Integer.toString(iSelfCode);
				
			//	System.out.println("TeCode+++++++++++++"+TeCode);
				//SelfCode=TeCode.substring((TeCode.length()-StepLeng),StepLeng);
				newColumnCode=ParentCode+TeCode;
			//	System.out.println("newColumnCode+++++++++++++"+newColumnCode);
			}
			else
			{
				newColumnCode=ParentCode;
				for(int n=0;n<(StepLeng-1);n++)
				{
					newColumnCode=newColumnCode+"0";
				}
				newColumnCode=newColumnCode+"1";
			}
		}
		
		//System.out.println("newColumnCode+++++++++++++"+newColumnCode);
		if(postid==null || postid.length()<=0 || postid.equals("null"))
		{
			
				sqlStr1="insert into `gyhy_column` ( `Name`,`ColumnCode`,`ColumnType`,`IsColumn`,`ColumnInfo`,`CountNum`) values('"+Name+"','"+newColumnCode+"','"+ColumnType+"','"+IsColumn+"','"+ColumnInfo+"','0');";
		}
		else
		{
				sqlStr1="update `gyhy_column` set `Name`='"+Name+"',`ColumnType`='"+ColumnType+"',`IsColumn`='"+IsColumn+"',`ColumnInfo`='"+ColumnInfo+"' " ;
				if(EditCode==true)
				{
					sqlStr1=sqlStr1+",`ColumnCode`='"+ColumnCode+"' ";
				}
				sqlStr1=sqlStr1+		"where `Id`="+postid;
		}
		
	//	System.out.println(sqlStr1);
		boolean isok=false;
		try {
			conn = DBManager.getConnection();
			conn.executeUpdate(sqlStr1);
			conn.close();
			isok=true;
		} catch (SQLException e) {
			isok=false;
			e.printStackTrace();
		}
		finally {
            conn.close();
		}
		if(isok==true)
		{
			return new ActionForward("/admin/admin_column.jsp",3,"更新成功");
		}
		else
		{
			return new ActionForward("/admin/admin_column.jsp",3,"更新失败");
		}
	}
	
	public ActionForward del(HttpServletRequest request, HttpServletResponse response)
	{
		
		try {
			request.setCharacterEncoding("UTF-8");
			response.setCharacterEncoding("utf-8");
		} 
		catch (UnsupportedEncodingException e1) 
		{
			
		}
		String postid=getV(request.getParameter("postid"));
		//System.out.println("postid+++++++++++++"+postid);
		
		PooledConnection conn = null;
		String sqlStr1="";
		
		sqlStr1="delete from  `gyhy_column` where `Id`="+postid;
		
		//System.out.println(sqlStr1);
		boolean isok=false;
		try {
			conn = DBManager.getConnection();
			conn.executeUpdate(sqlStr1);
			conn.close();
			isok=true;
		} catch (SQLException e) {
			isok=false;
			e.printStackTrace();
		}
		finally {
            conn.close();
		}
		if(isok==true)
		{
			return new ActionForward("/admin/admin_column.jsp",3,"更新成功");
		}
		else
		{
			return new ActionForward("/admin/admin_column.jsp",3,"更新失败");
		}
	}

	public static String TransactSQLInjection(String str)
	{
	    return str.replaceAll(".*([';]+|(--)+).*", " ");
	}

	private String getV(String S)
	{
		//System.out.println(S);
		String returnS;
		if(S==null || S.length()<=0)
		{
			returnS="";
		}
		else
		{
			returnS=S;
		}
		return TransactSQLInjection(returnS);
	}

}
